----------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------


AuctionSearchWindow =
{ 
	
    -- Constants
    -- TEMP: DEBUG: TODO: remove
    TEMP_3_4_INTERFACE = false,
    TEMP_4_1_INTERFACE = true,
    HIDE_USABLE_CHECKBOX = false,
    --USE_LEVEL_RANGE_TO_FAKE_CRAFTING_ITEM_SEARCH = true,
    
    DEBUG_ON = false,
    DEBUG_LEVEL = 6,
    
    SHOULD_LIMIT_COMBO_BOX_TO_SINGLE_CHOICE = false,
    
    MAX_TRADE_SKILL_LEVEL = 999,			-- currently max is 200, but leaving room for expnasion
    
    CHECKBOX_CHECKED_ICON = 57,
    CHECKBOX_UNCHECKED_ICON = 58,
    
    
	-- TODO: Eventually the server should let the client know when a player/guild
	--		 has unlocked Guild Auctions. Currently the client doesn't 
	--		have a way of telling this, so instead of hardcoding a guild rank
	--		that will change we simply show Guild Only when you're in a guild,
	--		and show Guild and Alliance once you're in an alliance. If your guild
	--		doesn't have the proper permissions, then the server will let you
	--		know why the auction did not succeed.
    REQUIRED_GUILD_RANK_FOR_GUILD_ONLY_SEARCHES = 1,
    

    -- Strings
    titleBarText                = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_TITLE ),
    rankLabel                   = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_RANK_LABEL ),
    rankRangeSeparatorSymbol    = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_RANK_SEPARATOR_SYMBOL ),
    tradeSkillLevelLabel        = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_CRAFTING_LEVEL_LABEL ),
    tradeSkillLevelRangeSeparatorSymbol    = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_CRAFTING_LEVEL_SEPARATOR_SYMBOL ),
    usableOnlyLabel             = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_USABLE_ONLY_LABEL ),
    maxPriceLabel               = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_MAX_PRICE_LABEL ),
    -- ignoring Min Price for now since it doesn't seem very important
    searchButtonText            = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_SEARCH_BUTTON ),
    resetButtonText             = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_RESET_BUTTON ),
     
    itemNameNotSetText          = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_ITEM_NAME_LABEL ),
    sellerNameNotSetText        = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_SELLER_NAME_LABEL ),
    --bidderNameNotSetText        = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_BIDDER_NAME_LABEL ),
    bidderNameNotSetText        = L"",
    
    rankNotSetText              = L"",
    tradeSkillLevelNotSetText	= L"",
    
    -- Restriction Type Strings
    RESTRICTION_TYPE_ANY_TEXT       = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_RESTRICTION_TYPE_ANY_TEXT ),
    RESTRICTION_TYPE_ALLIANCE_TEXT  = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_RESTRICTION_TYPE_ALLIANCE_TEXT ),
    RESTRICTION_TYPE_GUILD_TEXT     = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_RESTRICTION_TYPE_GUILD_TEXT ),
    
    careerChoiceNotSetText			= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_CAREER_NOT_SET ),
    rarityChoiceNotSetText			= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_RARITY_NOT_SET ),
    statModChoiceNotSetText			= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_STAT_NOT_SET ),
    rarityChoiceNotSetText			= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_RARITY_NOT_SET ),

    itemTypeChoiceNotSetText		= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_ITEM_TYPE_NOT_SET ),
    itemTypeChoiceAllWeaponsText    = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_ITEM_TYPE_ALL_WEAPONS ),
    itemTypeChoiceAllArmorText		= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_ITEM_TYPE_ALL_ARMOR ),
    itemTypeChoiceAllAccessoriesText = GetStringFromTable( "AuctionHouseStrings", StringTables.AuctionHouse.CREATE_SEARCH_ITEM_TYPE_ALL_ACCESSORIES ),
    itemTypeChoiceAllCraftingText	= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_ITEM_TYPE_ALL_CRAFTING ),
    itemTypeChoiceAllMiscText		= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_ITEM_TYPE_ALL_MISC ),

    slotChoiceNotSetText			= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_SLOT_NOT_SET ),
    slotChoiceAllWeaponsText		= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_SLOT_ALL_WEAPONS ),
    slotChoiceAllArmorText			= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_SLOT_ALL_ARMOR ),
    slotChoiceAllAccessoriesText	= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_SLOT_ALL_ACCESSORIES ),
    slotChoiceAllCraftingText		= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_SLOT_ALL_CRAFTING ),
    slotChoiceAllMiscText			= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_SLOT_ALL_MISC ),


	-- Category Strings
    categoryChoiceNotSetText		= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_CATEGORY_NOT_SET ),
    categoryChoiceWeaponsText		= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_CATEGORY_WEAPONS ),
    categoryChoiceArmorText			= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_CATEGORY_ARMOR ),
    categoryChoiceAccessoriesText   = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_CATEGORY_ACCESSORIES ),
    categoryChoiceCraftingText      = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_CATEGORY_CRAFTING ),
	categoryChoiceMiscText			= GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.CREATE_SEARCH_CATEGORY_MISC ),
    
	-- variables -- we may want to move these into AuctionListDataManager if we want to preserve each tabs values separately
	
	itemTypesSelected = {},
	itemSlotsSelected = {},
	statModsSelected = {},
	
    -- because not all RestrictionChoice values are displayed, we need to keep this as a mapping
    --   between the Combo Box indices and the RestrictionChoice data to be used
    RestrictionChoiceDisplayed = {},
		
	isShowingTradeSkillLevel = nil,		-- gets set to false during initialization, but has to be different here or it gets skipped
}


local function SendDebug( text, level )
	level = level or 5
	
    if AuctionSearchWindow.DEBUG_ON and level >= AuctionSearchWindow.DEBUG_LEVEL then
        DEBUG( text )
    end
end



    -- DefaultValue
    -- TODO: we should provide a way for the user to override these
    --   and save them off to a local file
AuctionSearchWindow.DefaultValue =
{ 
    CareerChoiceIndex = 1,
    RarityChoiceIndex = 1,
    ItemTypeChoiceIndex = 1,
    StatModChoiceIndex = 1,
    SlotChoiceIndex = 1,
    RestrictionChoiceIndex = 1,
    
    ItemNameText = AuctionSearchWindow.itemNameNotSetText,
    SellerNameText = AuctionSearchWindow.sellerNameNotSetText,
    BidderNameText = AuctionSearchWindow.bidderNameNotSetText,
    
    MinRank = AuctionSearchWindow.rankNotSetText,
    MaxRank = AuctionSearchWindow.rankNotSetText,
    MinTradeSkillLevel = AuctionSearchWindow.tradeSkillLevelNotSetText,
    MaxTradeSkillLevel = AuctionSearchWindow.tradeSkillLevelNotSetText,
    
    
    
    UsableOnly = false,
    MaxPrice = 0,
}


---------------------------------
-- Combo Box data tables 

-- Combo Box checkbox icons text
-- spaces reflect spacing not text, sono localization needed
AuctionSearchWindow.checkboxChecked = L"<icon"..AuctionSearchWindow.CHECKBOX_CHECKED_ICON..L">  "
AuctionSearchWindow.checkboxUnchecked = L"<icon"..AuctionSearchWindow.CHECKBOX_UNCHECKED_ICON..L">  "


-- comparator function to sort the below tables in alphabetical order
local function compareTextStrings(a,b) 
    --return( WStringToString( a.text ) < WStringToString( b.text ) )
    return( a.text < b.text )
end




-- Career
--  AuctionSearchWindow.CareerChoice requires some logic so it's created by AuctionSearchWindow.InitCareerChoiceTable( realm ) 

--  Item Rarity
AuctionSearchWindow.RarityChoice =
{ 
    {   text = AuctionSearchWindow.rarityChoiceNotSetText,                  value = SystemData.ItemRarity.UTILITY,      },  
    {   text = GameDefs.ItemRarity[SystemData.ItemRarity.COMMON].desc,      value = SystemData.ItemRarity.COMMON,       }, 
    {   text = GameDefs.ItemRarity[SystemData.ItemRarity.UNCOMMON].desc,    value = SystemData.ItemRarity.UNCOMMON,     },   
    {   text = GameDefs.ItemRarity[SystemData.ItemRarity.RARE].desc,        value = SystemData.ItemRarity.RARE,         },   
    {   text = GameDefs.ItemRarity[SystemData.ItemRarity.VERY_RARE].desc,   value = SystemData.ItemRarity.VERY_RARE,    },   
    {   text = GameDefs.ItemRarity[SystemData.ItemRarity.ARTIFACT].desc,    value = SystemData.ItemRarity.ARTIFACT,    },  
}



-- Item Type

local function CreateItemTypeChoiceList( itemTypeIndices, sortAlphabetically )

	local itemTypeChoices = {}
	for i, v in pairs( itemTypeIndices ) do
		if ItemTypes[v] ~= nil then
			table.insert( itemTypeChoices, { text = ItemTypes[v].name, value = v, isSelected = false, } )
		else
	SendDebug(L"AuctionSearchWindow.ItemTypeChoice[]: no name found for GameData.ItemTypes, value = "..v, 4)
		end    
	end

	if sortAlphabetically then
		table.sort( itemTypeChoices, compareTextStrings )
	end
	
	return itemTypeChoices
end


AuctionSearchWindow.ItemTypeChoice = {}

if AuctionSearchWindow.TEMP_4_1_INTERFACE then

	local itemIndices =
	{
		GameData.ItemTypes.AXE, 
		GameData.ItemTypes.BOW,  
		GameData.ItemTypes.DAGGER,
		GameData.ItemTypes.GUN, 
		GameData.ItemTypes.HAMMER,
		GameData.ItemTypes.PISTOL,
		GameData.ItemTypes.SPEAR, 
		GameData.ItemTypes.STAFF, 
		GameData.ItemTypes.SWORD, 

		GameData.ItemTypes.LIGHTARMOR,
		GameData.ItemTypes.MEDIUMARMOR,   
		GameData.ItemTypes.HEAVYARMOR,    
		GameData.ItemTypes.ROBE,
		GameData.ItemTypes.MEDIUMROBE,   
		GameData.ItemTypes.SHIELD,
		GameData.ItemTypes.CHARM, 
		-- TODO: Add Accessories here

		GameData.ItemTypes.CRAFTING,
		GameData.ItemTypes.DYE,
		GameData.ItemTypes.ENHANCEMENT,
		GameData.ItemTypes.POTION,
	}
	AuctionSearchWindow.DefaultItemTypes = CreateItemTypeChoiceList( itemIndices )
	table.insert( AuctionSearchWindow.DefaultItemTypes, 1, {   text = AuctionSearchWindow.itemTypeChoiceNotSetText, value = 0, isSelected = nil, } )

	
	itemIndices =		
	{
		GameData.ItemTypes.AXE, 
		GameData.ItemTypes.BOW,  
		GameData.ItemTypes.DAGGER,
		GameData.ItemTypes.GUN, 
		GameData.ItemTypes.HAMMER,
		GameData.ItemTypes.PISTOL,
		GameData.ItemTypes.SPEAR, 
		GameData.ItemTypes.STAFF, 
		GameData.ItemTypes.SWORD, 
	}
	AuctionSearchWindow.WeaponItemTypes = CreateItemTypeChoiceList( itemIndices, true )
	table.insert( AuctionSearchWindow.WeaponItemTypes, 1, {   text = AuctionSearchWindow.itemTypeChoiceAllWeaponsText, value = 0, isSelected = nil, } )

	
	itemIndices =	
	{
		GameData.ItemTypes.LIGHTARMOR,
		GameData.ItemTypes.MEDIUMARMOR,   
		GameData.ItemTypes.HEAVYARMOR,    
		GameData.ItemTypes.ROBE,
		GameData.ItemTypes.MEDIUMROBE,   
		GameData.ItemTypes.SHIELD,
		GameData.ItemTypes.CHARM, 
	}
	AuctionSearchWindow.ArmorItemTypes = CreateItemTypeChoiceList( itemIndices )
	table.insert( AuctionSearchWindow.ArmorItemTypes, 1, {   text = AuctionSearchWindow.itemTypeChoiceAllArmorText, value = 0, isSelected = nil, } )

	
	AuctionSearchWindow.AccessoryItemTypes = { }	
	table.insert( AuctionSearchWindow.AccessoryItemTypes, 1, {   text = AuctionSearchWindow.itemTypeChoiceAllAccessoriesText, value = 0, isSelected = nil, } )

	
	AuctionSearchWindow.CraftingItemTypes = { }	
	table.insert( AuctionSearchWindow.CraftingItemTypes, 1, {   text = AuctionSearchWindow.itemTypeChoiceAllCraftingText, value = 0, isSelected = nil, } )
	
	itemIndices =	
	{
		GameData.ItemTypes.POTION,
		GameData.ItemTypes.ENHANCEMENT,
		GameData.ItemTypes.DYE,
	}
	AuctionSearchWindow.MiscItemTypes = CreateItemTypeChoiceList( itemIndices )
	table.insert( AuctionSearchWindow.MiscItemTypes, 1, {   text = AuctionSearchWindow.itemTypeChoiceAllMiscText, value = 0, isSelected = nil, } )
	

	AuctionSearchWindow.ItemTypeChoice = AuctionSearchWindow.DefaultItemTypes


-- Item Types Not used
--[[ 	
		GameData.ItemTypes.BASICSHIELD   
		GameData.ItemTypes.EXPERTSHIELD  

		GameData.ItemTypes.XBOW        
		GameData.ItemTypes.LANCE         
		GameData.ItemTypes.REPEATINGXBOW 
		GameData.ItemTypes.THROWN,
		
		GameData.ItemTypes.TROPHY       
		GameData.ItemTypes.QUEST    
--]]

else

	AuctionSearchWindow.ItemTypeChoice = CreateItemTypeChoiceList( GameData.ItemTypes, true )
	AuctionSearchWindow.ItemTypeChoiceNotSet =  {   text = AuctionSearchWindow.itemTypeChoiceNotSetText, value = 0, isSelected = nil, }
	table.insert( AuctionSearchWindow.ItemTypeChoice, 1, AuctionSearchWindow.ItemTypeChoiceNotSet )
	
end

-- Slot
AuctionSearchWindow.SlotChoice = {}

local function CreateSlotChoiceList( slotIndices, sortAlphabetically )

	local slotChoices = {}
	for i, v in pairs( slotIndices ) do

		if ItemSlots[v] ~= nil then
		table.insert( slotChoices, { text = ItemSlots[v].name, value = v, isSelected = false, } )

	-- TEMP: SendDebug:
		else
			SendDebug(L"AuctionSearchWindow.SlotChoice[]: no name found for GameData.ItemSlots, value = "..v)
	-- END TEMP: SendDebug:
		end   
	end

	if sortAlphabetically then
		table.sort( slotChoices, compareTextStrings )
	end
	
	return slotChoices
end



if AuctionSearchWindow.TEMP_4_1_INTERFACE then

	-- Slots

	local slotIndices = 
	{
	
		GameData.ItemSlots.RIGHT_HAND, 
		GameData.ItemSlots.LEFT_HAND,  
		GameData.ItemSlots.EITHER_HAND,
		-- We want to add a fake value for BOTH_HANDS
		GameData.ItemSlots.RANGED,

		GameData.ItemSlots.BODY,  
		GameData.ItemSlots.BOOTS, 
		GameData.ItemSlots.GLOVES,
		GameData.ItemSlots.HELM,  
		GameData.ItemSlots.SHOULDERS,  
		
		GameData.ItemSlots.ACCESSORY1, 
		GameData.ItemSlots.BACK,  
		GameData.ItemSlots.BELT,  
	}
	AuctionSearchWindow.DefaultSlots = CreateSlotChoiceList( slotIndices )
	table.insert( AuctionSearchWindow.DefaultSlots, 1, { text = AuctionSearchWindow.slotChoiceNotSetText, value = 0, isSelected = nil, } )
	
	slotIndices = 
	{
		GameData.ItemSlots.RIGHT_HAND, 
		GameData.ItemSlots.LEFT_HAND,  
		GameData.ItemSlots.EITHER_HAND,
		-- We want to add a fake value for BOTH_HANDS
		GameData.ItemSlots.RANGED,
	}
	AuctionSearchWindow.WeaponSlots = CreateSlotChoiceList( slotIndices )
	table.insert( AuctionSearchWindow.WeaponSlots, 1, { text = AuctionSearchWindow.slotChoiceAllWeaponsText, value = 0, isSelected = nil, } )
	
	
	slotIndices = 
	{
		GameData.ItemSlots.BODY,  
		GameData.ItemSlots.GLOVES,
		GameData.ItemSlots.BOOTS, 
		GameData.ItemSlots.HELM,  
		GameData.ItemSlots.SHOULDERS,  
	}
	AuctionSearchWindow.ArmorSlots = CreateSlotChoiceList( slotIndices )
	table.insert( AuctionSearchWindow.ArmorSlots, 1, { text = AuctionSearchWindow.slotChoiceAllArmorText, value = 0, isSelected = nil, } )

	
	slotIndices = 
	{
		GameData.ItemSlots.BACK,  
		GameData.ItemSlots.BELT,  
		GameData.ItemSlots.ACCESSORY1, 
	}
	AuctionSearchWindow.AccessorySlots = CreateSlotChoiceList( slotIndices, true )
	table.insert( AuctionSearchWindow.AccessorySlots, 1, { text = AuctionSearchWindow.slotChoiceAllAccessoriesText, value = 0, isSelected = nil, } )
	
	
	AuctionSearchWindow.CraftingSlots = {}
	table.insert( AuctionSearchWindow.CraftingSlots, 1, { text = AuctionSearchWindow.slotChoiceAllCraftingText, value = 0, isSelected = nil, } )
	
	
	AuctionSearchWindow.MiscSlots = {}
	table.insert( AuctionSearchWindow.MiscSlots, 1, { text = AuctionSearchWindow.slotChoiceAllMiscText, value = 0, isSelected = nil, } )
	
	

-- Item Slots Not used
--[[ 	
		GameData.ItemSlots.BANNER,
		GameData.ItemSlots.SHIRT, 
		GameData.ItemSlots.PANTS, 
--]]

else

	AuctionSearchWindow.DefaultSlots = CreateSlotChoiceList( GameData.ItemSlots, true )
	AuctionSearchWindow.SlotChoiceNotSet =  { text = AuctionSearchWindow.slotChoiceNotSetText, value = 0, isSelected = nil, }
	table.insert( AuctionSearchWindow.DefaultSlots, 1, AuctionSearchWindow.SlotChoiceNotSet )

end


AuctionSearchWindow.SlotChoice = AuctionSearchWindow.DefaultSlots

    
-- Item Category 

local ASW = AuctionSearchWindow

ASW.categoryChoiceNotSetIndex		= 1
ASW.categoryChoiceWeaponsIndex		= 2
ASW.categoryChoiceArmorIndex		= 3
ASW.categoryChoiceAccessoriesIndex	= 4
ASW.categoryChoiceCraftingIndex  	= 5
ASW.categoryChoiceMiscIndex			= 6


if ASW.TEMP_4_1_INTERFACE then

	ASW.CategoryChoice =
	{
		[ASW.categoryChoiceNotSetIndex]		= { text=ASW.categoryChoiceNotSetText,			itemTypes=ASW.DefaultItemTypes,		slots=ASW.DefaultSlots,		},
		[ASW.categoryChoiceWeaponsIndex]	= { text=ASW.categoryChoiceWeaponsText,			itemTypes=ASW.WeaponItemTypes,		slots=ASW.WeaponSlots,		},
		[ASW.categoryChoiceArmorIndex]		= { text=ASW.categoryChoiceArmorText,			itemTypes=ASW.ArmorItemTypes,		slots=ASW.ArmorSlots,		},
		[ASW.categoryChoiceAccessoriesIndex] = { text=ASW.categoryChoiceAccessoriesText,	itemTypes=ASW.AccessoryItemTypes,	slots=ASW.AccessorySlots,	},
		[ASW.categoryChoiceCraftingIndex]  	= { text=ASW.categoryChoiceCraftingText,		itemTypes=ASW.CraftingItemTypes,	slots=ASW.CraftingSlots,	},
		[ASW.categoryChoiceMiscIndex]		= { text=ASW.categoryChoiceMiscText,			itemTypes=ASW.MiscItemTypes,		slots=ASW.MiscSlots,		},	
	}

end



-- Stat Modifier
AuctionSearchWindow.StatModChoice =
{ 
    --{   text = AuctionSearchWindow.statModChoiceNotSetText,                  value = BonusTypes[GameData.BonusTypes.EBONUS_NONE].name,      },  
    {   text = AuctionSearchWindow.statModChoiceNotSetText,						value = GameData.BonusTypes.EBONUS_NONE,			 isSelected = nil,	},  
    
    -- TODO: add an Other choice at the end to find abilities not of these main 11
    --    use table.insert after the sorting

    {   text = BonusTypes[GameData.BonusTypes.EBONUS_INITIATIVE].name,          value = GameData.BonusTypes.EBONUS_INITIATIVE, 		 isSelected = false,	}, 
    {   text = BonusTypes[GameData.BonusTypes.EBONUS_INTELLIGENCE].name,        value = GameData.BonusTypes.EBONUS_INTELLIGENCE, 	 isSelected = false,	}, 
    {   text = BonusTypes[GameData.BonusTypes.EBONUS_STRENGTH].name,            value = GameData.BonusTypes.EBONUS_STRENGTH, 		 isSelected = false,	}, 
    {   text = BonusTypes[GameData.BonusTypes.EBONUS_TOUGHNESS].name,           value = GameData.BonusTypes.EBONUS_TOUGHNESS, 		 isSelected = false,	}, 
    {   text = BonusTypes[GameData.BonusTypes.EBONUS_WILLPOWER].name,           value = GameData.BonusTypes.EBONUS_WILLPOWER, 		 isSelected = false,	}, 
    {   text = BonusTypes[GameData.BonusTypes.EBONUS_WOUNDS].name,              value = GameData.BonusTypes.EBONUS_WOUNDS, 			 isSelected = false,	}, 
    {   text = BonusTypes[GameData.BonusTypes.EBONUS_BALLISTICSKILL].name,      value = GameData.BonusTypes.EBONUS_BALLISTICSKILL, 	 isSelected = false,	}, 
    {   text = BonusTypes[GameData.BonusTypes.EBONUS_WEAPONSKILL].name,         value = GameData.BonusTypes.EBONUS_WEAPONSKILL,		 isSelected = false,	}, 
    {   text = BonusTypes[GameData.BonusTypes.EBONUS_CORPOREAL_RESIST].name,    value = GameData.BonusTypes.EBONUS_CORPOREAL_RESIST, isSelected = false,	}, 
    {   text = BonusTypes[GameData.BonusTypes.EBONUS_ELEMENTAL_RESIST].name,    value = GameData.BonusTypes.EBONUS_ELEMENTAL_RESIST, isSelected = false,	}, 
    {   text = BonusTypes[GameData.BonusTypes.EBONUS_SPIRIT_RESIST].name,       value = GameData.BonusTypes.EBONUS_SPIRIT_RESIST, 	 isSelected = false,	}, 
    
    -- These 4 from the original group are believed no longer used:
    --    EBONUS_DISRUPTSKILL, EBONUS_BLOCKSKILL, EBONUS_PARRYSKILL, EBONUS_EVADESKILL 
    -- or at least have been replace by:
    --    EBONUS_BLOCK EBONUS_PARRY EBONUS_EVADE EBONUS_DISRUPT
}   


 
--  Guild Only or Alliance Only searches

AuctionSearchWindow.RESTRICTION_NONE = 1
AuctionSearchWindow.RESTRICTION_ALLIANCE = 2
AuctionSearchWindow.RESTRICTION_GUILD = 3

AuctionSearchWindow.RestrictionChoice =
{ 
    [AuctionSearchWindow.RESTRICTION_NONE] =		{   text = AuctionSearchWindow.RESTRICTION_TYPE_ANY_TEXT,        value = GameData.Auction.RESTRICTION_NONE,    },  
    [AuctionSearchWindow.RESTRICTION_ALLIANCE] =	{   text = AuctionSearchWindow.RESTRICTION_TYPE_ALLIANCE_TEXT,   value = GameData.Auction.RESTRICTION_GUILD_ALLIANCE_ONLY,    },  
    [AuctionSearchWindow.RESTRICTION_GUILD] =		{   text = AuctionSearchWindow.RESTRICTION_TYPE_GUILD_TEXT,      value = GameData.Auction.RESTRICTION_GUILD_ONLY,    },  
}


----------------------------------------------------------------
-- Local Variables
----------------------------------------------------------------

--local windowName = "AuctionSearchWindow"
local windowName = "AuctionHouseWindowCreateSearch"

----------------------------------------------------------------
-- AuctionSearchWindow Functions
----------------------------------------------------------------

-- called directly by AuctionHouseWindow.Initialize()
function AuctionSearchWindow.Initialize()
SendDebug(L"AuctionSearchWindow.Initialize")
    
    -- Set all static data    
    LabelSetText( windowName.."TitleBarText", AuctionSearchWindow.titleBarText )
    LabelSetText( windowName.."RankHeader", AuctionSearchWindow.rankLabel )
    LabelSetText( windowName.."RankSeparator", AuctionSearchWindow.rankRangeSeparatorSymbol )
    LabelSetText( windowName.."TradeSkillLevelHeader", AuctionSearchWindow.tradeSkillLevelLabel )
    LabelSetText( windowName.."TradeSkillLevelSeparator", AuctionSearchWindow.tradeSkillLevelRangeSeparatorSymbol )

    AuctionSearchWindow.InitCareerChoiceTable( GameData.Player.realm ) 
	AuctionSearchWindow.SetComboBoxChoices( windowName.."Career", AuctionSearchWindow.CareerChoice )

    LabelSetText( windowName.."UsableOnlyHeader", AuctionSearchWindow.usableOnlyLabel )
    ButtonSetCheckButtonFlag( windowName.."UsableOnlyButton", true )
   
	AuctionSearchWindow.SetComboBoxChoices( windowName.."Category", AuctionSearchWindow.CategoryChoice )
	AuctionSearchWindow.SetComboBoxChoices( windowName.."ItemType", AuctionSearchWindow.ItemTypeChoice )
	AuctionSearchWindow.SetComboBoxChoices( windowName.."Slot", AuctionSearchWindow.SlotChoice )
	AuctionSearchWindow.SetComboBoxChoices( windowName.."StatMod", AuctionSearchWindow.StatModChoice )
	AuctionSearchWindow.SetComboBoxChoices( windowName.."Rarity", AuctionSearchWindow.RarityChoice )
	
    LabelSetText( windowName.."MaxPriceHeader", AuctionSearchWindow.maxPriceLabel )

    ButtonSetText( windowName.."SearchButton", AuctionSearchWindow.searchButtonText)
    ButtonSetText( windowName.."ResetButton", AuctionSearchWindow.resetButtonText )
    
    -- Reset dynamic data
    AuctionSearchWindow.Clear()
    
    -- TEMP: 
    if AuctionSearchWindow.HIDE_USABLE_CHECKBOX then
		WindowSetShowing( windowName.."UsableOnly", false )
    end
end



function AuctionSearchWindow.Shutdown()

end

function AuctionSearchWindow.OnShown()
SendDebug(L"AuctionSearchWindow.OnShown")

    --WindowUtils.OnShown()
    AuctionSearchWindow.Clear()
end

function AuctionSearchWindow.OnHidden()
SendDebug(L"AuctionSearchWindow.OnHidden")
    --WindowUtils.OnHidden()
end

function AuctionSearchWindow.OnTextSelected()
--SendDebug(L"AuctionSearchWindow.OnTextSelected")
    local textEditBoxName = SystemData.ActiveWindow.name
    TextEditBoxSelectAll( textEditBoxName )
SendDebug(L"AuctionSearchWindow.OnTextSelected, window = "..StringToWString(textEditBoxName))
end


-- choices is an array of tables. Each table entry needs to have a .text field
--   which will be used as a menu item
function AuctionSearchWindow.SetComboBoxChoices( comboBoxName, choices )
SendDebug( L"AuctionSearchWindow.SetComboBoxChoices for "..StringToWString(comboBoxName) )
    
    for index, data in ipairs( choices ) do
		if data.isSelected == true then
			ComboBoxAddMenuItem( comboBoxName, AuctionSearchWindow.checkboxChecked..data.text )
		elseif data.isSelected == false then
			ComboBoxAddMenuItem( comboBoxName, AuctionSearchWindow.checkboxUnchecked..data.text )
		else
			ComboBoxAddMenuItem( comboBoxName, data.text )
		end
	end
end



function AuctionSearchWindow.UpdateRestrictionTypes()
SendDebug(L"AuctionSearchWindow.UpdateRestrictionTypes")

	local inGuild = GuildWindow and GuildWindow.IsPlayerInAGuild()
	local inAlliance = inGuild and (GameData.Guild.Alliance.Id ~= 0)
	
	ComboBoxClearMenuItems( windowName.."Restriction" )
	AuctionSearchWindow.RestrictionChoiceDisplayed = {}
	
	local restriction = AuctionSearchWindow.RestrictionChoice[AuctionSearchWindow.RESTRICTION_NONE]
	ComboBoxAddMenuItem( windowName.."Restriction", restriction.text )
	table.insert( AuctionSearchWindow.RestrictionChoiceDisplayed, restriction )

	
	if AuctionSearchWindow.PlayerCanSearchGuildAuctions() then
		restriction = AuctionSearchWindow.RestrictionChoice[AuctionSearchWindow.RESTRICTION_GUILD]
		ComboBoxAddMenuItem( windowName.."Restriction", restriction.text )
		table.insert( AuctionSearchWindow.RestrictionChoiceDisplayed, restriction )
	end
	
	if AuctionSearchWindow.PlayerCanSearchAllianceAuctions() then
		restriction = AuctionSearchWindow.RestrictionChoice[AuctionSearchWindow.RESTRICTION_ALLIANCE]
		ComboBoxAddMenuItem( windowName.."Restriction", restriction.text )
		table.insert( AuctionSearchWindow.RestrictionChoiceDisplayed, restriction )
	end
	
    ComboBoxSetSelectedMenuItem( windowName.."Restriction", AuctionSearchWindow.DefaultValue.RestrictionChoiceIndex)
end


function AuctionSearchWindow.PlayerCanSearchGuildAuctions()

	local inGuild = GuildWindow and GuildWindow.IsPlayerInAGuild()
	local playerMeetsRequirement = ( inGuild and GameData.Guild.m_GuildRank and
									GameData.Guild.m_GuildRank >= AuctionSearchWindow.REQUIRED_GUILD_RANK_FOR_GUILD_ONLY_SEARCHES
								   )
	return playerMeetsRequirement
end

-- if you're high enough guild rank to be in alliance we let you search on Guild and Alliance auctions
function AuctionSearchWindow.PlayerCanSearchAllianceAuctions()

	local inGuild = GuildWindow and GuildWindow.IsPlayerInAGuild()
	local inAlliance = inGuild and (GameData.Guild.Alliance.Id ~= 0)
	
	return inAlliance
end


--  Career Choices (only show those for your realm)
function AuctionSearchWindow.InitCareerChoiceTable( realm ) 
    if realm == GameData.Realm.ORDER then

        AuctionSearchWindow.CareerChoice =
        { 
            {   text = CareerNames[GameDefs.CAREERID_IRON_BREAKER].name,    value = GameDefs.CAREERID_IRON_BREAKER, },  
            {   text = CareerNames[GameDefs.CAREERID_RUNE_PRIEST].name,     value = GameDefs.CAREERID_RUNE_PRIEST, },  
            {   text = CareerNames[GameDefs.CAREERID_ENGINEER].name,        value = GameDefs.CAREERID_ENGINEER, },  
            {	text = CareerNames[GameDefs.CAREERID_KNIGHT].name,			value = GameDefs.CAREERID_KNIGHT, },
            {   text = CareerNames[GameDefs.CAREERID_WITCH_HUNTER].name,    value = GameDefs.CAREERID_WITCH_HUNTER, },  
            {   text = CareerNames[GameDefs.CAREERID_BRIGHT_WIZARD].name,   value = GameDefs.CAREERID_BRIGHT_WIZARD, },  
            {   text = CareerNames[GameDefs.CAREERID_WARRIOR_PRIEST].name,  value = GameDefs.CAREERID_WARRIOR_PRIEST, },  
            {   text = CareerNames[GameDefs.CAREERID_SWORDMASTER].name,     value = GameDefs.CAREERID_SWORDMASTER, },  
            {   text = CareerNames[GameDefs.CAREERID_SHADOW_WARRIOR].name,  value = GameDefs.CAREERID_SHADOW_WARRIOR, },  
            {   text = CareerNames[GameDefs.CAREERID_ARCHMAGE].name,        value = GameDefs.CAREERID_ARCHMAGE, },   
            {	text = CareerNames[GameDefs.CAREERID_SEER].name,			value = GameDefs.CAREERID_SEER, },
            {   text = CareerNames[GameDefs.CAREERID_SLAYER].name,        value = GameDefs.CAREERID_SLAYER, },
        }

    elseif realm == GameData.Realm.DESTRUCTION then

        AuctionSearchWindow.CareerChoice =
        { 
            {   text = CareerNames[GameDefs.CAREERID_BLACKORC].name, value = GameDefs.CAREERID_BLACKORC, }, 
            {   text = CareerNames[GameDefs.CAREERID_SHAMAN].name, value = GameDefs.CAREERID_SHAMAN, }, 
            {   text = CareerNames[GameDefs.CAREERID_SQUIG_HERDER].name, value = GameDefs.CAREERID_SQUIG_HERDER, }, 
            {   text = CareerNames[GameDefs.CAREERID_WARRIOR].name, value = GameDefs.CAREERID_WARRIOR, }, 
            {   text = CareerNames[GameDefs.CAREERID_CHOSEN].name, value = GameDefs.CAREERID_CHOSEN, }, 
            {   text = CareerNames[GameDefs.CAREERID_ZEALOT].name, value = GameDefs.CAREERID_ZEALOT, }, 
            {   text = CareerNames[GameDefs.CAREERID_ASSASSIN].name, value = GameDefs.CAREERID_ASSASSIN, }, 
            {   text = CareerNames[GameDefs.CAREERID_BLOOD_PRIEST].name, value = GameDefs.CAREERID_BLOOD_PRIEST, }, 
            {   text = CareerNames[GameDefs.CAREERID_SORCERER].name, value = GameDefs.CAREERID_SORCERER, },  
            {	text = CareerNames[GameDefs.CAREERID_MAGUS].name, value = GameDefs.CAREERID_MAGUS, },
            {	text = CareerNames[GameDefs.CAREERID_SHADE].name, value = GameDefs.CAREERID_SHADE, },	-- Blackguard
            { text = CareerNames[GameDefs.CAREERID_CHOPPA].name, value = GameDefs.CAREERID_CHOPPA, },

        }

    else
    
        SendDebug(L"ERROR in AuctionSearchWindow.InitCareerChoiceTable: Player's Realm not set.")
        AuctionSearchWindow.CareerChoice = {}
    end     -- if realm ==

    -- sort alphabetic order
    table.sort( AuctionSearchWindow.CareerChoice, compareTextStrings )
    
    -- add the default to the top of the list
    AuctionSearchWindow.CareerChoiceNotSet =  {   text = AuctionSearchWindow.careerChoiceNotSetText, value = 0, }
    table.insert( AuctionSearchWindow.CareerChoice, 1, AuctionSearchWindow.CareerChoiceNotSet )

end

-- set/reset fields to their default values
function AuctionSearchWindow.Clear()
SendDebug(L"AuctionSearchWindow.Clear")
   
   
    -- field for basic string search 
	TextEditBoxSetText( windowName.."ItemName", AuctionSearchWindow.DefaultValue.ItemNameText )

    -- fields for character attribute filters
	AuctionSearchWindow.ShowTradeSkillLevelInput( false )	-- inits Rank and trade skill level, with rank initially showing
	ComboBoxSetSelectedMenuItem( windowName.."Career", AuctionSearchWindow.DefaultValue.CareerChoiceIndex )
    ButtonSetPressedFlag( windowName.."UsableOnlyButton", AuctionSearchWindow.DefaultValue.UsableOnly )
    
    -- fields for item attribute filters
    
    -- setting the category automatically sets the itemType and Slots
	ComboBoxSetSelectedMenuItem( windowName.."Category", AuctionSearchWindow.categoryChoiceNotSetIndex )
	AuctionSearchWindow.OnCategorySelected( AuctionSearchWindow.categoryChoiceNotSetIndex )		
	--AuctionSearchWindow.OnItemTypeSelected( 1 )
	--AuctionSearchWindow.OnSlotSelected( 1 )
	AuctionSearchWindow.OnStatModSelected( 1 )
    ComboBoxSetSelectedMenuItem( windowName.."Rarity", AuctionSearchWindow.DefaultValue.RarityChoiceIndex )

    -- fields for auction attribute filters
    MoneyFrame.FormatMoney( windowName.."MaxPrice", AuctionSearchWindow.DefaultValue.MaxPrice, MoneyFrame.SHOW_EMPTY_WINDOWS )
   	TextEditBoxSetText( windowName.."SellerName", AuctionSearchWindow.DefaultValue.SellerNameText )
   	--TextEditBoxSetText( windowName.."BidderName", AuctionSearchWindow.DefaultValue.BidderNameText )
   	AuctionSearchWindow.UpdateRestrictionTypes()
   	
	
end


function AuctionSearchWindow.ShowTradeSkillLevelInput( tradeSkillChoiceSelected )
SendDebug(L"AuctionSearchWindow.Show", 4)

	-- just return if proper windows are already shown/hidden
	if AuctionSearchWindow.isShowingTradeSkillLevel == tradeSkillChoiceSelected then
		return
	end
		
	AuctionSearchWindow.isShowingTradeSkillLevel = tradeSkillChoiceSelected
	
	WindowSetShowing( windowName.."RankHeader", not tradeSkillChoiceSelected )
	WindowSetShowing( windowName.."RankSeparator", not tradeSkillChoiceSelected )
	WindowSetShowing( windowName.."MinRank", not tradeSkillChoiceSelected )
	WindowSetShowing( windowName.."MaxRank", not tradeSkillChoiceSelected )

	WindowSetShowing( windowName.."TradeSkillLevelHeader", tradeSkillChoiceSelected )
	WindowSetShowing( windowName.."TradeSkillLevelSeparator", tradeSkillChoiceSelected )
	WindowSetShowing( windowName.."MinTradeSkillLevel", tradeSkillChoiceSelected )
	WindowSetShowing( windowName.."MaxTradeSkillLevel", tradeSkillChoiceSelected )

	TextEditBoxSetText( windowName.."MinRank", AuctionSearchWindow.DefaultValue.MinRank )
	TextEditBoxSetText( windowName.."MaxRank", AuctionSearchWindow.DefaultValue.MaxRank )
	TextEditBoxSetText( windowName.."MinTradeSkillLevel", AuctionSearchWindow.DefaultValue.MinTradeSkillLevel )
	TextEditBoxSetText( windowName.."MaxTradeSkillLevel", AuctionSearchWindow.DefaultValue.MaxTradeSkillLevel )

end



function AuctionSearchWindow.Show()
SendDebug(L"AuctionSearchWindow.Show")

    WindowSetShowing( windowName, true )
end

function AuctionSearchWindow.Hide()
SendDebug(L"AuctionSearchWindow.Hide")
    WindowSetShowing( windowName, false )
end

function AuctionSearchWindow.ToggleShowing()
SendDebug(L"AuctionSearchWindow.ToggleShowing")

    if ( AuctionSearchWindow.IsShowing() == true ) then
        AuctionSearchWindow.Hide()
    else
        AuctionSearchWindow.Show()
    end
end

function AuctionSearchWindow.IsShowing()
SendDebug(L"AuctionSearchWindow.IsShowing")
    return WindowGetShowing( windowName )
end

function AuctionSearchWindow.OnEnterKeyPressed()
SendDebug(L"AuctionSearchWindow.OnEnterKeyPressed", 4)
    if windowName == WindowGetParent( SystemData.ActiveWindow.name ) then
        AuctionSearchWindow.Search()
    end
end

function AuctionSearchWindow.Search()  
SendDebug(L"AuctionSearchWindow.Search", 4)

   	local itemName = TextEditBoxGetText( windowName.."ItemName" )
    if itemName == AuctionSearchWindow.itemNameNotSetText then
   	    itemName = L""
   	end
   	
    local minRankText = TextEditBoxGetText( windowName.."MinRank" )
   	if minRankText == AuctionSearchWindow.rankNotSetText then
        minRankText = L"0"
   	end
   	
    local maxRankText = TextEditBoxGetText( windowName.."MaxRank" )
    if maxRankText == AuctionSearchWindow.rankNotSetText then
        maxRankText = L"0"
   	end
   	
    local minTradeSkillLevelText = TextEditBoxGetText( windowName.."MinTradeSkillLevel" )
   	if minTradeSkillLevelText == AuctionSearchWindow.tradeSkillLevelNotSetText then
        minTradeSkillLevelText = L"0"
   	end
   	
    local maxTradeSkillLevelText = TextEditBoxGetText( windowName.."MaxTradeSkillLevel" )
    if maxTradeSkillLevelText == AuctionSearchWindow.tradeSkillLevelNotSetText then
        maxTradeSkillLevelText = L"0"
   	end
   	
   	local usableFlag = ButtonGetPressedFlag( windowName.."UsableOnlyButton" )	
   	local maxPrice = MoneyFrame.ConvertCurrencyToBrass ( windowName.."MaxPrice" )
    
	local careerIndex = ComboBoxGetSelectedMenuItem( windowName.."Career" )
    local rarityIndex = ComboBoxGetSelectedMenuItem( windowName.."Rarity")
 	local restrictionIndex = ComboBoxGetSelectedMenuItem( windowName.."Restriction" )
 	
	local slotIndex = ComboBoxGetSelectedMenuItem( windowName.."Slot" )
	local statModIndex = ComboBoxGetSelectedMenuItem( windowName.."StatMod" )
	
   	local sellerName = TextEditBoxGetText( windowName.."SellerName" )
   	if sellerName == AuctionSearchWindow.sellerNameNotSetText then
   		sellerName = L""
   	end
   	
	-- searches in Auctions tab default to only auctions player has placed
	if AuctionListDataManager.IsMyAuctionsPaneShowing() and sellerName == L"" then
		sellerName = GameData.Player.name
		SendDebug(L"   sellerName = GameData.Player.name", 4)
	end
   	
   	local bidderName = L""
   	-- searches in Bids tab default to only auctions player is high bidder
   	if AuctionListDataManager.IsMyBidsPaneShowing() then
   		bidderName = GameData.Player.name
   			SendDebug(L"   bidderName = GameData.Player.name", 4)
   	end

    local queryData = {}
    
    queryData.usableOnly = usableFlag
    queryData.minPrice = 0
    queryData.maxPrice = maxPrice
    
    
    queryData.minItemLevel = tonumber( minRankText )
    queryData.maxItemLevel = tonumber( maxRankText )
    
    queryData.minTradeSkillLevel = tonumber( minTradeSkillLevelText ) or 0
    queryData.maxTradeSkillLevel = tonumber( maxTradeSkillLevelText ) or 0
    
    if AuctionSearchWindow.isShowingTradeSkillLevel and
	   (queryData.minTradeSkillLevel > 0 or queryData.maxTradeSkillLevel > 0) then
		
		if queryData.minTradeSkillLevel == 0 then
			queryData.minTradeSkillLevel = 1
		end
		if queryData.maxTradeSkillLevel == 0 then
			queryData.maxTradeSkillLevel = AuctionSearchWindow.MAX_TRADE_SKILL_LEVEL
		end
    end
    
    queryData.minRarity = AuctionSearchWindow.RarityChoice[rarityIndex].value
    queryData.career = AuctionSearchWindow.CareerChoice[careerIndex].value
    queryData.restrictionType = AuctionSearchWindow.RestrictionChoiceDisplayed[restrictionIndex].value
    
    
SendDebug(L"AuctionSearchWindow.Search restrictionIndex="..restrictionIndex, 6)
SendDebug(L"  queryData.restrictionType="..queryData.restrictionType, 6)
    
    queryData.itemTypes = {}
	queryData.itemEquipSlots = {}
	queryData.itemBonuses = {}
    
    
    -- TODO: Remove this single choice combo box option
    if AuctionSearchWindow.SHOULD_LIMIT_COMBO_BOX_TO_SINGLE_CHOICE  then
		
		local itemTypeIndex = ComboBoxGetSelectedMenuItem( windowName.."ItemType" )
		if itemTypeIndex ~= 1 then
   			queryData.itemTypes[1] = AuctionSearchWindow.ItemTypeChoice[itemTypeIndex].value
   		end
   		
		if slotIndex ~= 1 then
   			queryData.itemEquipSlots[1] = AuctionSearchWindow.SlotChoice[slotIndex].value
   		end
	   	
		if statModIndex ~= 1 then
   			queryData.itemBonuses[1] = AuctionSearchWindow.StatModChoice[statModIndex].value
   		end
   		
   	else
   	
		for index, choiceData in pairs( AuctionSearchWindow.itemTypesSelected ) do
			
			if choiceData.value ~= 0 then
				table.insert( queryData.itemTypes, choiceData.value )
			end
		end
		
		for index, choiceData in pairs( AuctionSearchWindow.itemSlotsSelected ) do
			
			if choiceData.value ~= 0 then
				table.insert( queryData.itemEquipSlots, choiceData.value )
			end
		end
		
		for index, choiceData in pairs( AuctionSearchWindow.statModsSelected ) do
			
			if choiceData.value ~= 0 then
				table.insert( queryData.itemBonuses, choiceData.value )
			end
		end
   	
    end
    
    

    -- if a category is selected but neither any specific itemtypes or slots are picked
	local categoryIndex = ComboBoxGetSelectedMenuItem( windowName.."Category" )
	if( categoryIndex ~= AuctionSearchWindow.categoryChoiceNotSetIndex and
		#queryData.itemTypes == 0 and
		#queryData.itemEquipSlots == 0 ) then
		
	-- TODO: Make trad skill items and accessories an item type until then add them in
		if categoryIndex == AuctionSearchWindow.categoryChoiceCraftingIndex  and queryData.maxTradeSkillLevel == 0 then
			queryData.minTradeSkillLevel = 1
			queryData.maxTradeSkillLevel = AuctionSearchWindow.MAX_TRADE_SKILL_LEVEL
		
		elseif categoryIndex == AuctionSearchWindow.categoryChoiceAccessoriesIndex then
		
			for index, choiceData in pairs( AuctionSearchWindow.SlotChoice ) do
				
				if choiceData.value ~= 0 then
					table.insert( queryData.itemEquipSlots, choiceData.value )
				end
			end
			
		else
		 
			for index, choiceData in pairs( AuctionSearchWindow.ItemTypeChoice ) do
				
				if choiceData.value ~= 0 then
					table.insert( queryData.itemTypes, choiceData.value )
				end
			end
		end
		
	end
    
    queryData.itemName = itemName
    queryData.sellerName = sellerName
    queryData.bidderName = bidderName

    SendAuctionSearch( queryData.usableOnly,
                       queryData.minPrice,
                       queryData.maxPrice,
                       queryData.minItemLevel,
                       queryData.maxItemLevel,    
                       queryData.minRarity,
                       queryData.career,
                       queryData.restrictionType,
                       queryData.minTradeSkillLevel,
                       queryData.maxTradeSkillLevel, 
            
                       queryData.itemTypes,
                       queryData.itemEquipSlots,
                       queryData.itemBonuses,
                       
                       queryData.itemName,
                       queryData.sellerName,
                       queryData.bidderName
                     )
    
    AuctionSearchWindow.lastQuery = queryData
    
    -- NOTE: this creates an interconnection, to the AuctionListWindow.
    --	 we may want to make this call through the AuctionHouseWindow instead.
    AuctionListWindow.ClearCurrentResults()
end


function AuctionSearchWindow.OnCategorySelected( choiceIndex )
    SendDebug( L"OnCategorySelected choiceIndex="..choiceIndex, 4)
    
	categoryData = AuctionSearchWindow.CategoryChoice[choiceIndex]
	
	-- TODO: find out if we need to clear this selected lists first before changing the values they're referencing
	AuctionSearchWindow.ItemTypeChoice = categoryData.itemTypes
	AuctionSearchWindow.SlotChoice = categoryData.slots
	
    -- clear the item type and slot list
	AuctionSearchWindow.OnItemTypeSelected( 1 )
	AuctionSearchWindow.OnSlotSelected( 1 )
	
	AuctionSearchWindow.ShowTradeSkillLevelInput( (choiceIndex == AuctionSearchWindow.categoryChoiceCraftingIndex) )
end


function AuctionSearchWindow.OnItemTypeSelected( choiceIndex )
    SendDebug( L"onItemTypeSelected choiceIndex="..choiceIndex, 4)

	local choiceDataTable = AuctionSearchWindow.ItemTypeChoice
	local selectedChoices = AuctionSearchWindow.itemTypesSelected
	local comboBoxName = windowName.."ItemType"	
	
    AuctionSearchWindow.SetMultiComboBoxValue( choiceIndex, comboBoxName, selectedChoices, choiceDataTable )
end

function AuctionSearchWindow.OnSlotSelected( choiceIndex )
    SendDebug( L"onSlotSelected choiceIndex="..choiceIndex, 4)

	local choiceDataTable = AuctionSearchWindow.SlotChoice
	local selectedChoices = AuctionSearchWindow.itemSlotsSelected
	local comboBoxName = windowName.."Slot"
	
	AuctionSearchWindow.SetMultiComboBoxValue( choiceIndex, comboBoxName, selectedChoices, choiceDataTable )
end

function AuctionSearchWindow.OnStatModSelected( choiceIndex )
    SendDebug( L"onStatModSelected choiceIndex="..choiceIndex, 4)

	local choiceDataTable = AuctionSearchWindow.StatModChoice
	local selectedChoices = AuctionSearchWindow.statModsSelected
	local comboBoxName = windowName.."StatMod"
	
	AuctionSearchWindow.SetMultiComboBoxValue( choiceIndex, comboBoxName, selectedChoices, choiceDataTable )
end


function AuctionSearchWindow.SetMultiComboBoxValue( choiceIndex, comboBoxName, selectedChoices, choiceDataTable )
    SendDebug( L"SetMultiComboBoxValue choiceIndex="..choiceIndex, 4)

	if AuctionSearchWindow.SHOULD_LIMIT_COMBO_BOX_TO_SINGLE_CHOICE then
		return
	end
	
	local choiceData = choiceDataTable[choiceIndex]
	if choiceData == nil then
		return
	end
	

	if choiceIndex == 1 then	
	
		-- first choice is always the no restriction field, so just clear any existing choices
		AuctionSearchWindow.ClearAllMultiComboBoxSelections( selectedChoices )

	elseif selectedChoices[choiceIndex] ~= nil then
	
		-- removing a previously selected choice
		choiceData.isSelected = false
		selectedChoices[choiceIndex] = nil
		
	else
		
		-- adding a new selected choice
		choiceData.isSelected = true
		selectedChoices[choiceIndex] = choiceData 
	end
	
	ComboBoxClearMenuItems( comboBoxName )
	AuctionSearchWindow.SetComboBoxChoices( comboBoxName, choiceDataTable )
	
	AuctionSearchWindow.SetMultiComboBoxName( comboBoxName, selectedChoices, choiceDataTable[1].text )
	
	if choiceIndex > 1 then
		AuctionSearchWindow.lastComboBox = comboBoxName
		WindowRegisterEventHandler( AuctionSearchWindow.lastComboBox, SystemData.Events.L_BUTTON_UP_PROCESSED, "AuctionSearchWindow.ReopenComboBox" )
	end
	
end


function AuctionSearchWindow.ReopenComboBox()

	ComboBoxExternalOpenMenu( AuctionSearchWindow.lastComboBox )
	WindowUnregisterEventHandler( AuctionSearchWindow.lastComboBox, SystemData.Events.L_BUTTON_UP_PROCESSED )
end

function AuctionSearchWindow.SetMultiComboBoxName( comboBoxName, selectedChoices, defaultName )
	SendDebug( L"SetMultiComboBoxName", 4)

	local string = L""
	for i, choiceData in pairs( selectedChoices ) do
		if string == L"" then
			string = L""..choiceData.text
		else
			string = string..L", "..choiceData.text
		end
	end
	
	if string == L"" then
		-- if no choices selected, then use the default value, which is always in the first index
		string = L""..defaultName
	end
	
	ButtonSetText( comboBoxName.."SelectedButton", string )
end

function AuctionSearchWindow.ClearAllMultiComboBoxSelections( selectedChoices )
	SendDebug( L"ClearAllMultiComboBoxSelections", 4)

	for choiceIndex, choiceData in pairs( selectedChoices ) do
		choiceData.isSelected = false
		selectedChoices[choiceIndex] = nil
	end
end